package com.outsource.framework.util.partyc.ecaop.flow;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.amplatform.controlpanel.properties.GlobalConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.n3r.ecaop.client.EcAopClient;
import org.n3r.ecaop.client.EcAopMethod;
import org.n3r.ecaop.client.EcAopResult;
import org.n3r.ecaop.client.SignAlgorithm;

public class Flowget {

	private static final Log log = LogFactory.getLog(Flowget.class);
	
	private static String flow_apptx=GlobalConfig.getProperty("app.flow.apptx");
	private static String flow_method= GlobalConfig.getProperty("app.flow.method");
	private static String flow_isencryption = GlobalConfig.getProperty("app.flow.isencryption");
	private static String flow_dex = GlobalConfig.getProperty("app.flow.des");
	private static String flow_encrypt = GlobalConfig.getProperty("app.flow.encrypt");
	private static String flow_timeout = GlobalConfig.getProperty("app.flow.timeout");
	private static String flow_addr = GlobalConfig.getProperty("app.flow.addr");
	private static String flow_appkey = GlobalConfig.getProperty("app.flow.appkey");
	private static String flow_signSecurty = GlobalConfig.getProperty("app.flow.signSecurity");
	
	private static String getReturnResponse(String usernumber) {
		log.info("request get user flow usernumber::::::"+usernumber);
		Map<String,String> reqMap = new HashMap<String,String>();
		reqMap.put("usernumber", usernumber);
		reqMap.put("apptx", flow_apptx);
		EcAopClient ecAopClient = new EcAopClient(flow_addr,  flow_appkey,  flow_signSecurty);
		ecAopClient.setSignAlgorithm(SignAlgorithm.HmacMD5);
		ecAopClient.setTimeoutMillis(Integer.valueOf(flow_timeout));
		EcAopMethod ecAopMethod = ecAopClient.createEcAopMethod( flow_method, Map.class);
		ecAopMethod.setEncryption(Boolean.valueOf(flow_isencryption));
		ecAopMethod.setSecretAlgorithm( flow_dex);
		ecAopMethod.setSecretEncrypt( flow_encrypt);
		EcAopResult result = ecAopMethod.exec(reqMap);
		String responseReturn = result.getResponse();
		log.info("response string:::::::"+responseReturn);
		return responseReturn;
	}
	
	public static String getUserFolw(String usermobile){
		return getReturnResponse(usermobile);
	}
	
	public static FlowResponse FlowEntry(String responseString){
		ObjectMapper om = new ObjectMapper();
		FlowResponse obj = null;
		try {
			if(responseString.indexOf("respcode")!=-1&&responseString.indexOf("respdesc")!=-1)
				obj = null;
			else
				obj = om.readValue(responseString,FlowResponse.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
}
